<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

class Users extends Dispatcher {
  /**
   * elementos del usuario
   */
  private $uid;
  private $login;
  private $firstname;
  private $lastname;
  private $email;
  private $hide_email;
  private $password;
  private $sex;
  private $hide_sex;
  private $confirmCode;
  private $active;
  private $ahosts;
  private $created;
  private $changed;
  private $theme;
  private $language;
  private $login_info;
  private $user_roles;
  private $msg;

  function setUid($uid) { $this->uid = $uid; }
	function setLogin($login) { $this->login = $login; }
	function setFirstname($firstname) { $this->firstname = $firstname; }
	function setLastname($lastname) { $this->lastname = $lastname; }
	function setEmail($email) { $this->email = $email; }
	function setPassword($password) { $this->password = $password; }
	function setSex($sex) { $this->sex = $sex; }
	function setStatus($active) { $this->active = $active; }
	function setAhosts($ahosts) { $this->ahosts = $ahosts; }
	function setFrom($from) { $this->from = $from; }
	function setSubject($subject) { $this->subject = $subject; }
	function setBody($body) { $this->body = $body; }
	function setConfirmCode($confirmCode) { $this->confirmCode = $confirmCode; }
	function setEmailSubject($emailSubject) { $this->emailSubject = trim($emailSubject); }
	function setEmailBody($emailBody) { $this->emailBody = trim($emailBody); }
	function setLogin_info($login_info) { $this->login_info = $login_info; }
	function setUser_roles($user_roles) { $this->user_roles = $user_roles; }
	function setMsg($msg) {$this->msg = $msg; }


	function getUid() { return $this->uid; }
	function getLogin() { return $this->login; }
	function getFirstname() { return $this->firstname; }
	function getLastname() { return $this->lastname; }
	function getEmail() { return $this->email; }
	function getPassword() { return $this->password; }
	function getSex() { return $this->sex; }
	function getStatus() { return $this->active; }
	function getAhosts() { return $this->ahosts; }
	function getFrom() { return $this->from; }
	function getSubject() { return $this->subject; }
	function getBody() { return $this->body; }
	function getConfirmCode() { return $this->confirmCode; }
	function getEmailSubject() { return $this->emailSubject; }
	function getEmailBody() { return $this->emailBody; }
  function getLogin_info() { return $this->login_info; }
  function getUser_roles() { return $this->user_roles; }
	function getMsg() { return $this->msg; }

	function deleteUser() {
	  $rs = $this->getDb()->Execute("DELETE core_users_roles FROM core_users_roles, core_users WHERE core_users_roles.uid=core_users.uid AND core_users.login='".$this->getLogin()."' AND core_users.uid != '1'");
	  $rs = $this->getDb()->Execute("DELETE FROM core_users WHERE login='".$this->getLogin()."' AND uid != '1'");
	  if(!$rs) { $this->setMsg($this->getDb()->ErrorMsg()); } else { return true; }
	}

	function checkExists($op) {
    if ($op == 'modify') {
      $rs = $this->getDb()->GetRow("SELECT * FROM core_users WHERE email='".$this->getEmail()."' AND uid !='".$this->getUid()."'");
      if(!$rs) { return true; } else { return false; }
    } else {
      $value = ($op == 'login') ? $this->getLogin() : $this->getEmail();
      $rs = $this->getDb()->GetRow("SELECT * FROM core_users WHERE $op='$value'");
      if(!$rs) { $this->setMsg($this->getDb()->ErrorMsg()); return true; } else { return false; }
    }
  }

  function getRoles() {
    $rs = $this->getDb()->GetAll("SELECT * FROM core_roles WHERE rid > 2 ORDER BY FIELD(rid,3) DESC, name");
    if(!$rs) { $this->setMsg($this->getDb()->ErrorMsg()); } else { return $rs; }
  }

  function pw_encode($login, $password) {
    return sha1($login.$password);
  }

  function AddUser($userDetails) {
    $record['firstname'] = $userDetails['firstname'];
	  $record['lastname']  = $userDetails['lastname'];
	  $record['email']     = $userDetails['email'];
	  $record['login']     = $userDetails['login'];
	  $record['password']  = $this->pw_encode($userDetails['login'], $userDetails['password1']);
	  $record['sex']       = $userDetails['sex'];
	  $record['active']    = 1;
	  srand((double)microtime() * 1000000);
	  $record['confirm_code'] = sha1($userDetails['login'] . time() . rand(1, 1000000));
	  $record['created'] = date("Y-m-d H:i:s");
    $result = $this->getDb()->AutoExecute('core_users', $record, 'INSERT');
    $this->uid = $this->getDb()->Insert_Id();
    if (isset($userDetails['checkbox'])) {
      /* verify if user has admin previleges */
      if (in_array(3, array_keys($userDetails['checkbox']))) {
        $data['uid'] = $this->getUid();
        $data['rid'] = 3;
        $rs = $this->getDb()->AutoExecute('core_users_roles', $data, 'INSERT');
      } else {
        foreach (array_keys($userDetails['checkbox']) as $value) {
          $data['uid'] = $this->getUid();
          $data['rid'] = $value;
          $rs = $this->getDb()->AutoExecute('core_users_roles', $data, 'INSERT');
        }
      }
    }
  }

  function ModifyUser($userDetails) {
    $record['firstname'] = $userDetails['firstname'];
	  $record['lastname']  = $userDetails['lastname'];
	  $record['email']     = $userDetails['email'];
	  if ( $userDetails['password1'] != sha1('no change')) {
  	  $record['password']  =  $this->pw_encode($this->getLogin(), $userDetails['password1']);
	  }
	  $record['sex']       = $userDetails['sex'];
	  $record['active']    = ($this->getUid() == 1) ? 1 : $userDetails['status'];
	  $record['ahosts']    = ($this->getUid() == 1) ? null : str_replace(array("\r\n", "\r", "\n"), "|", $userDetails['ahosts']);
	  $record['changed']   = date("Y-m-d H:i:s");
	  $rs = $this->getDb()->AutoExecute('core_users', $record, 'UPDATE', "uid='".$this->getUid()."'");
	  $rs = $this->getDb()->Execute("DELETE FROM core_users_roles WHERE uid='".$this->getUid()."'");
	  if (isset($userDetails['checkbox'])) {
      /* verify if user has admin previleges */
      if (in_array(3, array_keys($userDetails['checkbox']))) {
        $data['uid'] = $this->getUid();
        $data['rid'] = 3;
        $rs = $this->getDb()->AutoExecute('core_users_roles', $data, 'INSERT');
      } else {
        foreach (array_keys($userDetails['checkbox']) as $value) {
          $data['uid'] = $this->getUid();
          $data['rid'] = $value;
          $rs = $this->getDb()->AutoExecute('core_users_roles', $data, 'INSERT');
        }
      }
    }
  }

  function getAll($login) {
    $rs = $this->getDb()->GetRow("SELECT * FROM core_users WHERE login='$login'");
    if(!$rs) { $this->setMsg($this->getDb()->ErrorMsg()); }
    else {
      $this->setUid($rs['uid']);
      $this->setLogin($rs['login']);
      $this->setFirstname($rs['firstname']);
      $this->setLastname($rs['lastname']);
      $this->setEmail($rs['email']);
      $this->setSex($rs['sex']);
      $this->setStatus($rs['active']);
      $this->setAhosts($rs['ahosts']);
    }
    $rs = $this->getDb()->GetAll("SELECT * FROM core_users_roles WHERE uid='".$this->getUid()."'");
    if(!$rs) { $this->setMsg($this->getDb()->ErrorMsg()); }
    else {
      $this->setUser_roles($rs);
    }
  }

} // end of class

?>